// userPageB/pages/searchpage/searchpage.js
const app = getApp()
const WXAPI = require('../../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: ' ', // 导航栏 中间的标题
      textColor: '#0D0400', // 标题颜色
      bgColor: '#fff', // 导航栏背景颜色
      isloading: false,
      isColor: true,
    },
    currentNavtab: 2,
    choseList:[
      {id: 2,value: '店名'},
      {id: 1,value: '卡名称'},
    ],
    name: '',
    lnt: '',
    lat:'',
    searchList: [],
    issearchpage: false,//
    assetsHistory: [],//资产历史
    storesHistory: [],//搜索历史
    isFocus: true,
    ishistory: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.assetsList()
    this.storesList()
    var assetsHistory = wx.getStorageSync('assetsHistory')
    var ishistory = this.data.ishistory
    console.log('assetsHistory',assetsHistory)
    if(assetsHistory.length> 0){
      ishistory =  true
    }else{
      ishistory = false
    }
    this.setData({
      lnt: app.globalData.lnt,
      lat: app.globalData.lat,
      ishistory: ishistory
    })
    console.log('lnt',this.data.lnt)
    console.log('lat',this.data.lat)
  },
  //值变化
  searchinput(e){
    var val = e.detail.value
    this.setData({
      name: val
    })
    if(val == ''){
      this.setData({
        issearchpage: false,
      })
    }else{
      this.setData({
        isFocus: false,
      })
    }
    console.log('val',val)

  },
  //搜索框聚焦
  searchFocus(){
    this.setData({
      isFocus: true,
    })
  },
  //失去焦点搜索
  searchblur(e){
   var val = e.detail.value
   if(val == ''){
    this.setData({
      isFocus: false,
    })
   }else{
    this.setData({
      isFocus: true,
    })
   }
   
  },
  handleSearch(){
    var name = this.data.name
    if(name !=''){
      this.getglobalSearch()
    }
  },
  //搜索
  getglobalSearch(){
    this.setData({
      searchList: []
    })
    var data = {
      type: this.data.currentNavtab,
      name: this.data.name,
      lnt: this.data.lnt,
      lat: this.data.lat,
    }
    WXAPI.globalSearch(data,(res)=>{
      console.log('搜索',res)
      const {result,code} = res
      this.setData({
        issearchpage: true,
        searchList: result
      })
      var assetsHistory = this.data.assetsHistory
      var storesHistory = this.data.storesHistory
        // console.log(searchhistory)
      if(this.data.currentNavtab == 1){
        if(assetsHistory.length >0){
          assetsHistory.unshift(this.data.name)
        }else{
          assetsHistory.push(this.data.name)
        }
        wx.setStorageSync('assetsHistory',assetsHistory)
        
        this.assetsList()
      }else if(this.data.currentNavtab == 2){
        if(storesHistory.length >0){
          storesHistory.unshift(this.data.name)
        }else{
          storesHistory.push(this.data.name)
        }
        wx.setStorageSync('storesHistory',storesHistory)
        this.storesList()
      }
        
    })
  },
  //删除搜索历史
  delete(){
    if(this.data.currentNavtab == 1){
      wx.removeStorageSync('assetsHistory')
      this.assetsList()
    }else if(this.data.currentNavtab == 2){
      wx.removeStorageSync('storesHistory')
      this.storesList()
    }
    
  },
  assetsList(){
    var arr = wx.getStorageSync('assetsHistory')
    arr = this.arrayNonRepeatfy(arr)
    if(arr){
      if(arr.length > 21){
        this.setData({
          assetsHistory: arr.slice[0,20]
        })
      }else{
        this.setData({
          assetsHistory: arr
        })
      }
    }else{
      this.setData({
        assetsHistory: []
      })
    }
  },
  storesList(){
    var  arr = wx.getStorageSync('storesHistory')
    arr = this.arrayNonRepeatfy(arr)
    if(arr){
      if(arr.length > 21){
        this.setData({
          storesHistory: arr.slice[0,20]
        })
      }else{
        this.setData({
          storesHistory: arr
        })
      }
    }else{
      this.setData({
        storesHistory: []
      })
    }
  },
  arrayNonRepeatfy(arr) {
    let map = new Map();
    let array = new Array();  // 数组用于返回结果
    for (let i = 0; i < arr.length; i++) {
      if(map.has(arr[i])) {  // 如果有该key值
        map.set(arr[i], true); 
      } else { 
        map.set(arr[i], false);   // 如果没有该key值
        array.push(arr[i]);
      }
    } 
    return array ;
  },
  historyClick(e){
    var value = e.currentTarget.dataset.value
    this.setData({
      name: value
    })
  },
  //取消
  handleCancel(){
    this.setData({
      name: '',
      issearchpage: false,
    })
  },
  TableClick(e){
    var id =  e.currentTarget.dataset.id;
    this.setData({
      currentNavtab: id
    })
    if(this.data.name){
      this.getglobalSearch()
    }
  },
  handleDetail(e){
    console.log('this.currentNavtab',this.data.currentNavtab)
    var id =  e.currentTarget.dataset.id;
    var assetType  =  e.currentTarget.dataset.type;
    if(this.data.currentNavtab == 2){
      wx.navigateTo({
        url: '/userPageB/pages/userStore/storesDetails/storesDetails?shopId='+id,
      })
    }
    if(this.data.currentNavtab == 1){
      if(assetType == 1){
        wx.navigateTo({
          url: '/userPageB/pages/userStore/integralDetail/integralDetail?id='+id+'&assetType='+assetType,
        })
      }else if(assetType == 2){
        wx.navigateTo({
          url: '/userPageB/pages/userStore/vouchersDetail/vouchersDetail?id='+id+'&assetType='+assetType,
        })
      }
      else if(assetType == 3){
        wx.navigateTo({
          url: '/userPageB/pages/userStore/cardDetail/cardDetail?assetId='+id,
        })
      }
      
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})